package controller;
 

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import socket.Server;
import util.Log;
 
@Component
public class StartupListener implements ApplicationContextAware, ServletContextAware,
        InitializingBean, ApplicationListener<ContextRefreshedEvent> {
	
	@Resource
	Server server;
 
    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        Log.outLog("1 => StartupListener.setApplicationContext");
//        new Thread(new Runnable() {
//			@Override
//			public void run() {
//				server.start();
//			}
//		}).start();
    }
 
    @Override
    public void setServletContext(ServletContext context) {
        Log.outLog("2 => StartupListener.setServletContext");
    }
 
    @Override
    public void afterPropertiesSet() throws Exception {
        Log.outLog("3 => StartupListener.afterPropertiesSet");
    }
 
    @Override
    public void onApplicationEvent(ContextRefreshedEvent evt) {
    	
      new Thread(new Runnable() {
			@Override
			public void run() {
				server.start();
			}
		}).start();
      
//      new Thread(new Runnable() {
//			@Override
//			public void run() {
//				sendCommand.start();
//			}
//		}).start();
    	
        Log.outLog("4.1 => MyApplicationListener.onApplicationEvent");
        if (evt.getApplicationContext().getParent() == null) {
            Log.outLog("4.2 => MyApplicationListener.onApplicationEvent");
        }
    }
 
}